#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

import time

from openchord import URL,TcpEndPoint,CallInListener,TcpProxy

from openchord.help import logging

class   Listener(CallInListener) :
    def __init__(self) :
        self.logger = logging.getLogger('OpenChord')
        ## __init__()

    def callin(self, method, parameters) :
        #self.logger.info('CallIn(method=%s parameters="%s"', method, parameters)
        return parameters
        ## callin()
    ## class Listener

url_str = 'octcp://127.0.0.1:4300/test'

url = URL(url_str)

logger = logging.getLogger('OpenChord')

logger.debug('create TcpEndPoint(url=%s) ...', str(url))
endpoint = TcpEndPoint(url, Listener())
endpoint.open()

logger.debug('create TcpProxy(url=%s) ...', str(url))
proxy = TcpProxy(url)

#### ------------------------------------------------------------- ####

logger.debug('Proxy.callout ...')
method = 'echo'
parameters = 'hello,OpenChord.'
response = proxy.callout(method, parameters)
logger.info('CallOut(method=%s parameters=%s) return %s', method, parameters, response)

logger.debug('Proxy.callout ...')
method = 'echo'
parameters = 'hello,OpenChord.'
response = proxy.callout(method, parameters)
logger.info('CallOut(method=%s parameters=%s) return %s', method, parameters, response)

times = 10000
time_start = time.time()
for i in range(times) :
    #logger.debug('Proxy.callout ...')
    method = 'echo'
    parameters = 'hello,OpenChord.'
    response = proxy.callout(method, parameters)
    #logger.info('CallOut(method=%s parameters=%s) return %s', method, parameters, response)
time_last = time.time() - time_start
tps = times / time_last 
logger.info('%d callout times last %.2f seconds, tps=%.3f', times, time_last, tps)

#### ------------------------------------------------------------- ####

logger.debug('destroy TcpEndPoint ...')
endpoint.close()

logger.debug('done.')

